package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqSolidliquidProduceMaintenanceWeekMapper;
import com.guofen.pojo.SjqSolidliquidProduceElectricity;
import com.guofen.pojo.SjqSolidliquidProduceMaintenanceDay;
import com.guofen.pojo.SjqSolidliquidProduceMaintenanceWeek;
import com.guofen.service.sjq.SjqSolidliquidProduceMaintenanceWeekService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SjqSolidliquidProduceMaintenanceWeekServiceImpl implements SjqSolidliquidProduceMaintenanceWeekService {

    @Autowired
    private SjqSolidliquidProduceMaintenanceWeekMapper sjqSolidliquidProduceMaintenanceWeekMapper;

    @Override
    public boolean add_recorder(String equipment_name, String operator, String maintenance_content, String remarks) {
        //获取当前日期
        String date = DateAndTimeUtil.getWeekDate();
        //获取计划保养日期
        String maintenance_time_plan = "2022-01-01";
        //获取设备保养实施日期
        String maintenance_time_implement = DateAndTimeUtil.getDate();
        //设备是否正常
        String is_normal;
        if (remarks == null || remarks.equals("")){
            is_normal = "正常";
        }else {
            is_normal = "异常";
        }
        SjqSolidliquidProduceMaintenanceWeek sjqSolidliquidProduceMaintenanceWeek = new SjqSolidliquidProduceMaintenanceWeek();
        sjqSolidliquidProduceMaintenanceWeek.setDate(date);
        sjqSolidliquidProduceMaintenanceWeek.setMaintenanceTimePlan(maintenance_time_plan);
        sjqSolidliquidProduceMaintenanceWeek.setMaintenanceTimeImplement(maintenance_time_implement);
        sjqSolidliquidProduceMaintenanceWeek.setEquipmentName(equipment_name);
        sjqSolidliquidProduceMaintenanceWeek.setMaintenanceContent(maintenance_content);
        sjqSolidliquidProduceMaintenanceWeek.setIsNormal(is_normal);
        sjqSolidliquidProduceMaintenanceWeek.setRemarks(remarks);
        sjqSolidliquidProduceMaintenanceWeek.setOperator(operator);
        int result = sjqSolidliquidProduceMaintenanceWeekMapper.insert(sjqSolidliquidProduceMaintenanceWeek);
        if (result == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean add_examiner(Integer id, String examiner, String remarks) {
        SjqSolidliquidProduceMaintenanceWeek sjqSolidliquidProduceMaintenanceWeek = sjqSolidliquidProduceMaintenanceWeekMapper.selectByPrimaryKey(id);
        sjqSolidliquidProduceMaintenanceWeek.setExaminer(examiner);
        sjqSolidliquidProduceMaintenanceWeek.setRemarks(remarks);
        int result = sjqSolidliquidProduceMaintenanceWeekMapper.updateByPrimaryKeySelective(sjqSolidliquidProduceMaintenanceWeek);
        if (result == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public SjqSolidliquidProduceMaintenanceWeek getlatest(String equipment_name, String maintenance_content) {
        return sjqSolidliquidProduceMaintenanceWeekMapper.getlatest(equipment_name, maintenance_content);

    }

    @Override
    public List<Integer> is_last_complete(String equipmentsName, String menstanceNum, String role) {
        List<Integer> res = new ArrayList<Integer>();
        String[] equipments = equipmentsName.split("_");
        String[] num = menstanceNum.split("_");
        boolean breakkey = false;

        for(int i=0;i<equipments.length ;i++){
            breakkey = false;
            List<SjqSolidliquidProduceMaintenanceWeek> items = sjqSolidliquidProduceMaintenanceWeekMapper.getlastByName(equipments[i]);
            if (items.size() == Integer.parseInt(num[i])){
                if(role.equals("examiner")){
                    for(int j=0;j<items.size();j++){
                        if(items.get(j).getExaminer()==null||items.get(j).getExaminer().equals("")){
                            res.add(1);
                            breakkey = true;
                            break;
                        }
                    }
                }
                if(breakkey)continue;
                res.add(2);
            }else{
                res.add(0);
            }
        }
        return res;
    }

    @Override
    public List<SjqSolidliquidProduceMaintenanceWeek> getWeekRecords() {
        return sjqSolidliquidProduceMaintenanceWeekMapper.getWeekRecords();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqSolidliquidProduceMaintenanceWeek> list = sjqSolidliquidProduceMaintenanceWeekMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqSolidliquidProduceMaintenanceWeek> list = sjqSolidliquidProduceMaintenanceWeekMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqSolidliquidProduceMaintenanceWeekMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqSolidliquidProduceMaintenanceWeek record) {
        return sjqSolidliquidProduceMaintenanceWeekMapper.updateByPrimaryKeySelective(record);
    }
}
